<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;

class UserBehavior extends Controller
{
    public function showbehavior(){
        $essay_id=input("essay_id");
        $user_behavior_type=input("user_behavior_type");
        $data=Db::name("user_behavior")->where(['essay_id'=>$essay_id,'user_behavior_type'=>$user_behavior_type])->select();
        return ['status'=>1,'message'=>'获取成功','data'=>$data];
    }

    public function addbehavior(){
        $data=input();
        $user_username=$data['user_username'];
        unset($data['user_username']);
        $data['user_id']=Db::name("user")->where("user_username",$user_username)->value("user_id");
        $behavior_count=$data['behavior_count'];
        unset($data['behavior_count']);
        if($behavior_count=='一次'){
            if(Db::name('user_behavior')->where(['essay_id'=>$data['essay_id'],'user_id'=>$data['user_id']])->count()>=1){
                return ['status'=>0,'message'=>'你已执行此操作'];
            }
        }
        if(Db::name("user_behavior")->insert($data)){
            $user_behavior_id=Db::name("user_behavior")->getLastInsID();
            return ['status'=>1,'message'=>'操作成功','user_behavior_id'=>$user_behavior_id];
        }else
            return ['status'=>0,'message'=>'操作失败'];
    }

    public function updatebehavior(){
        $data=input();
        if(Db::name("user_behavior")->where('user_behavior_id',$data['id'])->setField($data['column'],$data['value']))
            return ['status'=>1,'message'=>'更新成功'];
        else
            return ['status'=>0,'message'=>'更新失败'];
    }

    public function deletebehavior(){
        $user_behavior_id=input("user_behavior_id");
        if(Db::name("user_behavior")->where("user_behavior_id",$user_behavior_id)->delete())
            return ['status'=>1,'message'=>'操作成功'];
        else
            return ['status'=>0,'message'=>'操作失败'];
    }

    public function countbehavior(){
        $essay_id=input('essay_id');
        $user_behavior_type=input('user_behavior_type');
        $count=Db::name('user_behavior')->where(['essay_id'=>$essay_id,'user_behavior_type'=>$user_behavior_type])->count();
        return ['status'=>1,'message'=>'获取成功','data'=>$count];
    }

}